'use strict';
var _ = require('lodash');
var numberJs = require('@/libs/number');
function ajaxController($scope){
    var text = `    // lodash 全面替换我之前的工具库
        //////////////////////////////////////////////////////////////////////////////
        // 随机数字
        // =====================
        // liuyp 2019年6月9日17:28:26 lodash替换
        //////////////////////////////////////////////////////////////////////////////
        _bs(_.random(0, 5) ,result_flag_here
        _bs(_.random(5) ,result_flag_here
        _bs(_.random(5, true) ,result_flag_here
        _bs(_.random(1.2, 5.2) ,result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // UUID
        // =====================
        // liuyp 2019年6月9日22:00:05
        //////////////////////////////////////////////////////////////////////////////
        _bs(numberJs.uuid(10,5) ,result_flag_here
        _bs(numberJs.uuid(32) ,result_flag_here
        _bs(numberJs.uuid(32,5) ,result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // 从18位/17位身份证号算出校验位
        //////////////////////////////////////////////////////////////////////////////
        _bs(numberJs.idCardNumCompute('13013019870131001?') == 'X',result_flag_here
        _bs(numberJs.idCardNumCompute('13013019880602212?') == '9',result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // 判断身份证号是否正确
        //////////////////////////////////////////////////////////////////////////////
        _bs(numberJs.isCardID('13013019870131001x') == true,result_flag_here
        _bs(numberJs.isCardID('130130198701310024') == false,result_flag_here
        _bs(numberJs.cardIDInfo('13013019870131001x') ,result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // IP校验格式
        //////////////////////////////////////////////////////////////////////////////
        _bs(numberJs.validateIP('192.168.0.31') ,result_flag_here
        _bs(numberJs.validateIP('192.168.1.30') ,result_flag_here
        _bs(numberJs.validateIP('172.30.30.23') ,result_flag_here
        _bs(numberJs.validateIP('8.8.8.8') ,result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // 比较IP先后
        //////////////////////////////////////////////////////////////////////////////
        _bs(numberJs.compareIP('1.2.3.4','1.2.3.6') ,result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // 数字估算
        // 源,过滤数组
        // 为了适应浏览器局限宽度来显示数字,需要对数字进行估算显示
        //////////////////////////////////////////////////////////////////////////////
        _bs(numberJs.estimate(12.33) == "12",result_flag_here
        _bs(numberJs.estimate(123456) == "12万",result_flag_here
        _bs(numberJs.estimate(12345) == "1.2万",result_flag_here
        _bs(numberJs.estimate(1234) == "1234",result_flag_here
        _bs(numberJs.estimate(123) == "123",result_flag_here
        _bs(numberJs.estimate(12) == "12",result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // 字节计算
        //////////////////////////////////////////////////////////////////////////////
        _bs(numberJs.parseBytes(10000) == "9.766 KBytes" ,result_flag_here
        _bs(numberJs.parseBytes(10000,'MBytes') == "0.010 MBytes" ,result_flag_here
        _bs(numberJs.parseBytes(10000,'',1) == "9.766KB" ,result_flag_here
        _bs(numberJs.parseBytes(10000,'',1,2) == "9.77KB" ,result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // 数字转换为简写汉字
        // =====================
        // liuyp 2019年1月22日19:49:31
        //////////////////////////////////////////////////////////////////////////////
        _bs(numberJs.number2Chinese(2.67) == '二点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(23.67) == '二十三点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(20.67) == '二十点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(234.67) == '二百三十四点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(204.67) == '二百零四点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(2345.67) == '二千三百四十五点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(2040.67) == '二千零四十点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(23456.67) == '二万三千四百五十六点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(20006.67) == '二万零六点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(234567.67) == '二十三万四千五百六十七点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(204007.67) == '二十万四千零七点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(2345678.67) == '二百三十四万五千六百七十八点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(20450070.67) == '二千零四十五万零七十点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(23456789.67) == '二千三百四十五万六千七百八十九点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(20202020.67) == '二千零二十万二千零二十点六七' ,result_flag_here
        _bs(numberJs.number2Chinese(5533) == '五千五百三十三' ,result_flag_here
        _bs(numberJs.number2Chinese(5533.1) == '五千五百三十三点一' ,result_flag_here
        _bs(numberJs.number2Chinese(5533.04) == '五千五百三十三点零四' ,result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // 金额转换为汉字大写
        // 适用场景 不含小数且最多处理到万级别
        // =====================
        // liuyp 2019年1月22日19:49:31
        //////////////////////////////////////////////////////////////////////////////
        _bs(numberJs.money2Chinese(2.67) == '贰圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(23.67) == '贰拾叁圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(20.67) == '贰拾圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(234.67) == '贰佰叁拾肆圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(204.67) == '贰佰零肆圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(2345.67) == '贰仟叁佰肆拾伍圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(2040.67) == '贰仟零肆拾圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(23456.67) == '贰万叁仟肆佰伍拾陆圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(20006.67) == '贰万零陆圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(234567.67) == '贰拾叁万肆仟伍佰陆拾柒圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(204007.67) == '贰拾万零肆仟零柒圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(2345678.67) == '贰佰叁拾肆万伍仟陆佰柒拾捌圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(20450070.67) == '贰仟零肆拾伍万零柒拾圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(23456789.67) == '贰仟叁佰肆拾伍万陆仟柒佰捌拾玖圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(20202020.67) == '贰仟零贰拾万零贰仟零贰拾圆陆角柒分' ,result_flag_here
        _bs(numberJs.money2Chinese(5533) == '伍仟伍佰叁拾叁圆整' ,result_flag_here
        _bs(numberJs.money2Chinese(5533.1) == '伍仟伍佰叁拾叁圆壹角' ,result_flag_here
        _bs(numberJs.money2Chinese(5533.04) == '伍仟伍佰叁拾叁圆肆分' ,result_flag_here
        _bs(numberJs.money2Chinese(1191.86) == '壹仟壹佰玖拾壹圆捌角陆分' ,result_flag_here
        _bs(numberJs.money2Chinese(891.86) == '捌佰玖拾壹圆捌角陆分' ,result_flag_here
        _bs(numberJs.money2Chinese(891.80) == '捌佰玖拾壹圆捌角' ,result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // 数字计算
        // 为了避免以后可能的计算精度问题
        // 所有的数字业务必须使用工具库来计算
        //////////////////////////////////////////////////////////////////////////////
        // 原生代码计算浮点数 就是一坨shit
        _bs(0.1+ 0.2 ,result_flag_here
        _bs(6.6+1.32+1.2+1.2+1.2 ,result_flag_here
        _bs(6.6-1.32-1.2-1.2-1.2 ,result_flag_here
        _bs(6.6*1.31 ,result_flag_here
        _bs(6.6/1.32 ,result_flag_here
        _bs(1.1/10 ,result_flag_here
        _bs(1.1/100 ,result_flag_here
        // ludash 寄予厚望 依旧是一坨shit 不再尝试
        _bs(_.add(0.1, 0.2) ,result_flag_here
        _bs(_.sum([6.6, 1.32, 1.2, 1.2, 1.2]) ,result_flag_here
        // 只好使用很久以前收集的精准四则运算方法
        _bs(numberJs.add(0.1, 0.2) == 0.3,
        _bs(numberJs.add(0.3, 3) == 3.3,result_flag_here
        _bs(numberJs.add(0.9, 0.1) == 1,result_flag_here
        _bs(numberJs.add(0.11, 0.2) == 0.31,result_flag_here
        _bs(numberJs.add(2, 4) == 6,result_flag_here
        _bs(numberJs.add(121212, 131313) == 252525,result_flag_here
        _bs(numberJs.add(10000, 0.05) == 10000.05,result_flag_here
        _bs(numberJs.add(6.6, 1.32, 1.2, 1.2, 1.2) == 11.52,result_flag_here
        _bs(numberJs.sub(6.6, 1.32, 1.2, 1.2, 1.2) == 1.68,result_flag_here
        _bs(numberJs.sub(6.6, 1) == 5.6,result_flag_here
        _bs(numberJs.sub(12, 1) == 11,result_flag_here
        _bs(numberJs.sub(5, 1.2) == 3.8,result_flag_here
        _bs(numberJs.sub(100, 20) == 80,result_flag_here
        _bs(numberJs.sub(1000, 1) == 999,result_flag_here
        _bs(numberJs.sub(6.656, 0) == 6.656,result_flag_here
        _bs(numberJs.sub(6.656, 6.656) == 0,result_flag_here
        _bs(numberJs.mul(6.6, 1.32) == 8.712,result_flag_here
        _bs(numberJs.mul(1, 1.32) == 1.32,result_flag_here
        _bs(numberJs.mul(5, 10) == 50,result_flag_here
        _bs(numberJs.mul(1.11, 5) == 5.55,result_flag_here
        _bs(numberJs.mul(100, 100) == 10000,result_flag_here
        _bs(numberJs.mul(0.01, 100) == 1,result_flag_here
        _bs(numberJs.mul(0.1, 0.1) == 0.01,result_flag_here
        _bs(numberJs.div(6.6, 1.32) == 5,result_flag_here
        _bs(numberJs.div(1.1, 10) == 0.11,result_flag_here
        _bs(numberJs.div(100, 5) == 20,result_flag_here
        _bs(numberJs.div(50, 25) == 2,result_flag_here
        _bs(numberJs.div(250, 0.25) == 1000,result_flag_here
        _bs(numberJs.div(100, 100) == 1,result_flag_here
        _bs(numberJs.div(0, 1) == 0,result_flag_here
        _bs(numberJs.div(500, 1000) == 0.5,result_flag_here
        _bs(numberJs.div(6.6, 6.6) == 1,result_flag_here
        _bs(numberJs.div(6.666, 6) == 1.111,result_flag_here
        // 请注意 传递字符串类型也不会报错 只会转成对应浮点进行计算
        _bs(numberJs.add('2', '4') == 6,result_flag_here
        _bs(numberJs.sub('12', '1') == 11,result_flag_here
        _bs(numberJs.mul('5', '10') == 50,result_flag_here
        _bs(numberJs.div('50', '25') == 2,result_flag_here
        //////////////////////////////////////////////////////////////////////////////
        // 数字比较
        // 关于“字符串比对的规范”，案例：
        // 案例1：金额为1,999.00元，因为没有对 逗号做处理造成支付不成功；
        // 案例2：小时7:00与21:00比对，因处理不当造成21:00小于7:00
        // 要求
        //////////////////////////////////////////////////////////////////////////////
        _bs('7' < '21',result_flag_here
        _bs(parseInt('1999.00'),result_flag_here
        _bs(parseInt('1,999.00'),result_flag_here
        _bs(numberJs.lt('7', '21') ,result_flag_here
        _bs(numberJs.lt('1', '11') ,result_flag_here
        _bs(numberJs.lt('4', '22') ,result_flag_here
        _bs(numberJs.lt('12', '13') ,result_flag_here
        _bs(numberJs.lt('15', '51') ,result_flag_here
        _bs(numberJs.gt('21:00', '7:00') ,result_flag_here
        _bs(numberJs.gt('11:00', '7:00') ,result_flag_here
        _bs(numberJs.gt('21:30', '21:21') ,result_flag_here
        _bs(numberJs.gt('21:11', '7:11') ,result_flag_here
        _bs(numberJs.eq('1,999.00', '1999') === true,result_flag_here
        _bs(numberJs.eq('1,000.00', '1000') === true,result_flag_here
        _bs(numberJs.eq('1,999,999.00', '1999999') === true,result_flag_here
    `;
    var dotest = function(code){
        try {
            var r = eval("("+code+")");
            if( r !== true && code.indexOf('==') > -1 ){ return '异常 ' + eval("("+ code.replace(/\=\=.*/, '') +")") }
            return r
        } catch(e){
            console.error("单元测试代码错误", e)
            return "UNITTESTERROR"
        } };
    text = text.replace(/\_bs\((.*)\,result\_flag\_here/g,function(a,b,c){
        var result = dotest(b);
        if( result === true ){ result = "UNITTESTOK" }
        return "UNITTESTTEST "+b+" UNITTESTRESULT "+ JSON.stringify(result)
    })
    $("#templateAPI").html(
        text.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n[ ]{4}/g, '\n')
            .replace(/UNITTESTTEST/g, '<font color="#3333ff">测试代码: ')
            .replace(/UNITTESTRESULT/g, '</font><font color="#3333cc">结果</font>')
            .replace(/UNITTESTOK/g, '<span style="color: #4caf50;">单元测试通过</span>')
            .replace(/UNITTESTERROR/g, '<span style="color: #ff3300;">代码错误</span>')
    );
}
exports = module.exports = ajaxController;